package com.yc.product.dao;

import com.yc.common.util.DBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductDao {
    public Map<String, Object> queryPage() {
        String sql = "select * from dm_product where 1=1";
        List<Map<String, Object>> list = DBHelper.selectListMap(sql);
        long count = DBHelper.count(sql);
        Map<String, Object> page = new HashMap<>();
        page.put("rows", list);
        page.put("total", count);
        return page;
    }

    public Map<String, Object> queryPage(String name, String date1, String date2, int ipage, int isize) {
        String sql = "select * from dm_product where 1=1";
        List<Object> params = new ArrayList();
        if (name != null && name.trim().isEmpty() == false) {
            name = "%" + name + "%";
            sql += " and (pname like ? or cid like ? or pdesc like ? ) ";
            params.add(name);
            params.add(name);
            params.add(name);
        }
        if (date1 != null && date1.trim().isEmpty() == false) {
            sql += " and createtime >= ?";
            params.add(date1);
        }
        if (date2 != null && date2.trim().isEmpty() == false) {
            sql += " and createtime <= ?";
            params.add(date2);
        }

        ipage--;
        String sql1 = sql + " limit ?,?";
        String sql2 = sql;
        long count = DBHelper.count(sql2, params.toArray());
        params.add(ipage * isize);
        params.add(isize);
        List<Map<String, Object>> list = DBHelper.selectListMap(sql1, params.toArray());
        Map<String, Object> data = new HashMap<>();

        data.put("rows", list);
        data.put("total", count);

        System.out.println(data);
        return data;
    }

}
